package com.spdg.ring;

import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import cn.wolf.http.HttpCallBack;
import cn.wolf.tools.AlertHelper;
import cn.wolf.tools.CommonUtils;
import cn.wolf.tools.SharedPreferencesUtil;

import com.spdg.ring.bo.MenuBo;
import com.spdg.ring.common.Key;
import com.spdg.ring.datamgr.ArticleDataMgr;
import com.spdg.ring.datamgr.MenuDataMgr;
import com.spdg.ring.resp.MenuResp;
import com.spdg.ring.sqlite.model.Menu;

/**
 * <p>Title: 欢迎界面 </p>
 * <p>Description:  </p>
 * <p>@author: caijj                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-6             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class WelcomeActivity extends RingBaseActivity {

	MenuBo menuConfigBo;

	private Handler asyncHandler = new Handler() {
		public void dispatchMessage(Message msg) {
			requestMenuData();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SystemInit.init(getApplicationContext()); // 系统配置初始化

	}

	@Override
	protected void initComponents() {
	}

	@Override
	protected void initData() {

		boolean netWorkAvailable = CommonUtils.isNetConnectionAvailable(mContext);
		// 第一次安装，且网络异常，无法进入应用
		if (!netWorkAvailable && !installFirst()) {
			netWorkError();
			return;
		}
		
		if (!installFirst()) {
			SharedPreferencesUtil.setBoolean(mContext, Key.K_2G3G_IMAGE_SETTING, true);
		}

		mThread.start();
	}

	private void requestMenuData() {
		// 本地有菜单数据，直接跳转
		List<Menu> menus = MenuDataMgr.getInstance().getMenus(mContext);
		if (menus != null && menus.size() > 0) {
			switchToSplash();
		}
		
		// 网络正常，异步刷新菜单
		if (CommonUtils.isNetConnectionAvailable(mContext)) { 
			menuConfigBo = new MenuBo(mContext, new MenuCall());
			menuConfigBo.request();
		}
	}

	AsyncInit mThread = new AsyncInit();

	class AsyncInit extends Thread {
		@Override
		public void run() {
			MenuDataMgr.getInstance().loadDataSqlite(mContext);
			ArticleDataMgr.getInstance().loadDataSqlite(mContext);

			asyncHandler.sendEmptyMessage(0);
		}
	}

	class MenuCall implements HttpCallBack<MenuResp> {
		@Override
		public void call(MenuResp response) {

			// 跳转到首页
			switchToSplash();
			updateInstallFirst(); // 更新为已安装
		}

		@Override
		public void progress(Object... obj) {
		}

		@Override
		public void onNetWorkError() {
			netWorkError();
		}
	}

	// 网络异常，弹出对话框
	private void netWorkError() {
		String message = mContext.getString(R.string.network_error);
		AlertHelper.showMessage(mContext, message, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				exitApp(); // 退出应用

				finish();
			}
		});
	}

	/**
	 * 第一次安装  true:已安装， false:是
	 * @return
	 */
	private boolean installFirst() {
		return SharedPreferencesUtil.getBoolean(mContext, Key.K_SP_INSTALL_FIRST);
	}

	private void updateInstallFirst() {
		SharedPreferencesUtil.setBoolean(mContext, Key.K_SP_INSTALL_FIRST, Boolean.TRUE);
	}

	private void switchToSplash() {
		Intent intent = new Intent();
		intent.setClass(mContext, SplashActivity.class);
		startActivity(intent);

		finish();
	}

	@Override
	protected int getMainContentViewId() {
		return R.layout.act_welcome;
	}

	@Override
	protected Class<?> getResouceClass() {
		return R.class;
	}

	@Override
	protected String getEventID() {
		return getString(R.string.event_activity_welcome);
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (menuConfigBo != null) {
			menuConfigBo.cancle();
		}
	}

	@Override
	public void onBackPressed() {
	}

}
